//
//  StaffViewController.m
//  Marketing
//
//  Created by wmm on 16/3/11.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//部门详情 某个部门的人员

#import "StaffViewController.h"
#import "DepartmentModel.h"
#import "UserModel.h"
#import "SelectStaffViewCell.h"
#import "StaffDetailViewController.h"
#import "DepartModifyViewController.h"
#import "MJRefresh.h"
#define STARTX [UIView getWidth:20]

@interface StaffViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSMutableArray     *_userArray;
    
    BOOL   isRefre;
    
    NSString *depName;
    NSString *responMan;
    
    int cnamId;
}

@property (strong, nonatomic) UITableView *departStaffTableView;


@end

@implementation StaffViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationItem.title = _depart.name;
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:_depart.name];
    depName = _depart.name;
    responMan = _depart.cname;
//    cnamId = _depart.
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithAction:@selector(popViewController)];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"设置" style:UIBarButtonItemStyleBordered target:self action:@selector(clickToSetDepartment)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    _userArray = [NSMutableArray array];
    [self initTableView];
//    [self initData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
//    [_departStaffTableView reloadData];
    if (isRefre) {
        [_departStaffTableView .mj_header beginRefreshing];
        isRefre = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)initData{
    [_userArray removeAllObjects];
     [SVProgressHUD show];
    NSDictionary * params = [NSDictionary dictionaryWithObjectsAndKeys:@(UID), @"uid", TOKEN, @"token", @(_depart.departId), @"department", nil];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 20;
    [manager POST:GET_USER_BY_DEPARTMENT_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.view hideToastActivity];
        
        NSLog(@"GET_USER_BY_DEPARTMENT_URL:%@",responseObject);
        NSArray * departArray = [DataTool changeType:[(NSDictionary *)responseObject objectForKey:@"data"]];
        NSLog(@"%@",departArray);
        for (NSDictionary *user  in departArray) {
            UserModel *userModel = [[UserModel alloc]init];
            [userModel setValuesForKeysWithDictionary:user];
            [_userArray addObject:userModel];
        }
        [_departStaffTableView reloadData];
        [_departStaffTableView.mj_header endRefreshing];
        
          [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       [SVProgressHUD dismiss];
         [_departStaffTableView.mj_header endRefreshing];
        NSLog(@"分为非 %@",error);
    }];
}

- (void)initTableView{
    _departStaffTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH-64)];
    _departStaffTableView.dataSource = self;
    _departStaffTableView.delegate = self;
    _departStaffTableView.separatorStyle = UITableViewCellSelectionStyleNone;//去掉分割线
    _departStaffTableView.backgroundColor = graySectionColor;
    _departStaffTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //        [weakSelf loadNewData];
        [self initData];
        
    }];
    [_departStaffTableView.mj_header beginRefreshing];
    
    [self.view addSubview:_departStaffTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickToSetDepartment{
    DepartModifyViewController *departModifyViewController = [[DepartModifyViewController alloc] init];
    
    departModifyViewController.depart = _depart;
    departModifyViewController.departN = depName;
    departModifyViewController.cnameee = responMan;
    departModifyViewController.departMansArray = _userArray;
    [self.navigationController pushViewController:departModifyViewController animated:YES];
    __weak StaffViewController *staVc = self;
    departModifyViewController.blcok = ^(BOOL isRefresh,NSString *departName,NSString *cname,int cnameID,DepartmentModel *model){
        model.departId = _depart.departId;
        model.ids = _depart.ids;
        model.img = _depart.img;
        _depart = model;
        isRefre = isRefresh;
        staVc.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:departName];
        depName = departName;
        responMan = cname;
        NSLog(@"是否要刷新…、%d",isRefresh);
    };
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _userArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SelectStaffViewCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    SelectStaffViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        UserModel *user = _userArray[indexPath.row];
        cell = [[SelectStaffViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier withUser:user];
    }

        if(indexPath.row == _userArray.count - 1){
            UIView *vie = [ViewTool getLineViewWith:CGRectMake(0, [SelectStaffViewCell cellHeight] - 1, KSCreenW, 1) withBackgroudColor:grayLineColor];
            [cell addSubview:vie];
        }
  
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StaffDetailViewController *staffDetailViewController = [[StaffDetailViewController alloc] init];
    UserModel *user = _userArray[indexPath.row];
    
    staffDetailViewController.user = user;
    [self.navigationController pushViewController:staffDetailViewController animated:YES];
}

@end
